layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func','laydate'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var laydate = layui.laydate;


    //渲染时间选择框
    laydate.render({
        elem: '#month',
        format: 'yyyyMM',
        type: 'month',
        tiger: 'click',
        done: function(value){
            //alert(value);
            $('#month').val(value);
            Notice.search();
        }
    });

    /**
     * 系统管理--消息管理
     */
    var Notice = {
        tableId: "noticeTable"    //表格id
    };
    /**
     * 检索店名
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyWage/getDm");
    var languageResult = languageAjax.start();
    $.each(languageResult, function( index, val ) {
        $("#dm").append("<option value='"+val.dm+"'>"+val.dm+"</option>");
    } );
    form.render('select');

    /**
     * 初始化表格的列
     */
    Notice.initColumn = function () {
        return [[
            //{type: 'checkbox', fixed: 'left'},
            {field: 'id', align: "center", hide: true, sort: true, title: 'id'},
            {type: 'numbers',align:'center', title: '序号'},
            {field: 'dm', align: "center", sort: false, title: '店名'},
            {field: 'hp', align: "center", sort: false, title: '货赔金额'},
             {field: 'flag', align: "center", title: '月份'},
            // {field: 'createrName', align: "center", sort: true, title: '发布者'},
            // {field: 'createTime', align: "center", sort: true, title: '创建时间'},
            //{align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]];
    };

    /**
     * 点击清空按钮
     */
    Notice.reset = function () {
        $('input').val('');
        $("#status").val('');
        $("#dm").val('');
        form.render();
        $(function () {
            var tableResult = table.render({
                elem: '#' + Notice.tableId,
                url: Feng.ctxPath + '/btyWage/hpList',
                totalRow:true,	//开启合计行
                page: true,
                height: "full-98",
                cellMinWidth: 100,
                limit: 20,
                cols: Notice.initColumn(),
                done:function (res) {
                    // if (res.data == '') {
                    //     $('.layui-table-box').css('overflow', 'auto')
                    //     $('.layui-table-header, .layui-table-body').css('overflow', 'visible')
                    // }
                }
            });
        })
    };



    /**
     * 点击查询按钮
     */
    Notice.search = function () {
        var queryData = {};
        queryData['dm'] = $("#dm").val();
        queryData['month'] = $("#month").val();
        table.reload(Notice.tableId, {
            where: queryData, page: {curr: 1}
        });
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Notice.tableId,
        url: Feng.ctxPath + '/btyWage/hpList',
        totalRow:true,	//开启合计行
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        limit: 20,
        cols: Notice.initColumn(),
        done:function (res) {
            if (res.data == '') {
                $('.layui-table-box').css('overflow', 'auto')
                $('.layui-table-header, .layui-table-body').css('overflow', 'visible')
            }
        }
    });

    /**
     * 导出excel按钮
     */
    Notice.exportExcel = function () {
        //获取导出Excel的数据
        var ajax = new $ax(Feng.ctxPath + "/btyWage/listHpExport");
        var dm = $("#dm").val();
        var month = $("#month").val();
        ajax.set("month",month);
        ajax.set("dm",dm);
        var result = ajax.start();
            var lists=result;
        // for (index in lists) {
        //     var bh = lists[index].bh;
        //     var tel = lists[index].tel;
        //     lists[index].bh = bh + '\t';
        //     lists[index].tel = tel + "\t";
        // }
            table.exportFile(tableResult.config.id, lists, 'xls');
    };

    // 清空按钮点击事件
    $('#btnReset').click(function () {
        Notice.reset();
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Notice.search();
    });

    $('#btnUp').click(function () {
        Notice.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Notice.openAddNotice();
    });
    // 确认按钮点击事件
    $('#btnOk').click(function () {
        Notice.confirmOk();
    });
    // 取消审核按钮点击事件
    $('#btnQx').click(function () {
        Notice.confirmQx();
    });

    // 导出按钮点击事件
    $('#btnExp').click(function () {
        Notice.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Notice.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Notice.onEditNotice(data);
        } else if (layEvent === 'delete') {
            Notice.onDeleteNotice(data);
        }
    });
});
